package snake.demo.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import snake.demo.obj.BodyObj;
import snake.demo.obj.FoodObj;
import snake.demo.obj.HeadObj;
import snake.demo.util.GameUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;


/**
 * @author A
 * @version 1.0
 * @description: 游戏窗口
 * @date 2022/10/21 17:15
 */
public class GameWin extends JFrame {
    //游戏状态 0:未开始 1:游戏中 2:暂停 3:失败 4:通关 5:游戏失败重新开始 6:通关后关卡
    public static int state = 0;
    //分数
    public int score = 0;
    //窗口大小
    int winWidth = 800;
    int winHeight = 600;
    //定义双缓存图片
    Image offScreenImage = null;
    //蛇头对象
    HeadObj headObj = new HeadObj(GameUtils.rightImg, 60, 60, this);
    //蛇身体集合
    public List<BodyObj> bodyObjList = new ArrayList<>();
    //食物对象
    public FoodObj foodObj = new FoodObj().getFood();

    public void launch() {

        //设置窗口可见
        this.setVisible(true);
        //设置窗口大小
        this.setSize(winWidth, winHeight);
        //设置窗口的位置在屏幕的位置
        this.setLocationRelativeTo(null);
        //设置窗口标题

        this.setTitle("贪吃蛇");

        //对蛇身体进行初始化
        bodyObjList.add(new BodyObj(GameUtils.bodyImg, 30, 60, this));
        bodyObjList.add(new BodyObj(GameUtils.bodyImg, 0, 60, this));

        //键盘事件
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                    switch (state) {
                        //游戏状态 0:未开始 1:游戏中 2:暂停 3:失败 4:通关 5:游戏失败重新开始 6:通关后进入下一关卡
                        case 0:
                            state = 1;
                            break;
                        case 1:
                            state = 2;
                            repaint();
                            break;
                        case 2:
                            state = 1;
                            break;
                        case 3:
                            state = 5;
                            break;
                        case 4:
                            state =6;
                            break;
                        default:
                            break;
                    }
                }
            }
        });


        while (true) {
            if (state == 1) {
                repaint();
            }
            if (state == 5) {
                state = 0;
                //游戏失败重置游戏
                resetGame();
            }
            if (state ==6&&GameUtils.level!=3){
                state = 1;
                //游戏失败重置游戏
                GameUtils.level++;
                resetGame();
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void paint(Graphics g) {
        //初始化双缓存图片
        if (offScreenImage == null) {
            offScreenImage = this.createImage(winWidth, winHeight);

        }
        //获取图片对应的Graphics对象
        Graphics imageGraphics = offScreenImage.getGraphics();
        //灰色背景
        imageGraphics.setColor(Color.gray);

        imageGraphics.fillRect(0, 0, winWidth, winHeight);
        imageGraphics.setColor(Color.BLACK);

        //网格线
        for (int i = 0; i < 20; i++) {
            //横线
            imageGraphics.drawLine(0, i * 30, 600, i * 30);
            //竖线
            imageGraphics.drawLine(i * 30, 0, i * 30, 600);
        }
        //绘制蛇身体
        for (int i = bodyObjList.size() - 1; i >= 0; i--) {
            bodyObjList.get(i).paintSelf(imageGraphics);
        }
        //绘制蛇头
        headObj.paintSelf(imageGraphics);
        foodObj.paintSelf(imageGraphics);


        /**
         * @author: chensiwei
         * @date: 2022/10/26 15:50
         * 绘制分数(必须最后绘制不然被背景覆盖了)
         **/
        GameUtils.drawWord(imageGraphics, score + " 分", Color.BLUE, 50, 650, 300);

        GameUtils.drawWord(imageGraphics, "第"+GameUtils.level+"关", Color.BLUE, 50, 630, 200);
        //绘制提示语
        prompt(imageGraphics);
        //将双缓存图片绘制到窗口中
        g.drawImage(offScreenImage, 0, 0, null);
    }

    //绘制提示语
    void prompt(Graphics g) {
        //未开始
        if (state == 0) {
            //绘制矩形填充
            g.fillRect(120, 240, 400, 70);
            GameUtils.drawWord(g, "按下空格开始", Color.GREEN, 35, 150, 290);

        }
        //暂停
        if (state == 2) {
            //绘制矩形填充
            g.fillRect(120, 240, 400, 70);
            GameUtils.drawWord(g, "暂停中", Color.GREEN, 35, 150, 290);

        }
        //暂停
        if (state == 3) {
            //绘制矩形填充
            g.fillRect(80, 240, 500, 70);
            GameUtils.drawWord(g, "游戏失败,按空格重新开始", Color.GREEN, 35, 80, 290);

        }

        //通关
        if (state == 4) {
            if (GameUtils.level==3) {
                //绘制矩形填充
                g.fillRect(120, 240, 400, 70);
                GameUtils.drawWord(g, "达成目标,游戏通关", Color.GREEN, 35, 150, 290);
            }else {
                //绘制矩形填充
                g.fillRect(120, 240, 400, 70);
                GameUtils.drawWord(g, "达成目标,下一关", Color.GREEN, 35, 150, 290);
            }

        }


    }

    //游戏重置
    void resetGame() {
        //关闭当前窗口
        this.dispose();

        //开启新窗口
        String[] args = {};
        main(args);
    }

    public static void main(String[] args) {
        new GameWin().launch();
    }

}
